from django.http import HttpResponse
from django.http import HttpRequest
from Response.Resp import Resp
import json
from Model.models import User
from Model.models import AccessRecords


class Query(object):
    @classmethod
    def queryPersonalInfo(self, request: HttpRequest) -> HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                user = User.objects.get(id=uid)
                accrss_record = AccessRecords.objects.get(uid_id=user.id)
                score = accrss_record.final_score
                if score==None:
                    score = 0
                resp_json_data = {
                    'email': user.email,
                    'birth': user.birth,
                    'age':user.age,
                    'major':user.major,
                    'username':user.name,
                    'sex':user.sex,
                    'rescode': 200,
                    'score': score,
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)
